package net.kem.android.positioner.responsehandler;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class ResponseHandler implements ContentHandler {
	public static final int STATUS_OK = 0;
	protected String userName;
	protected int status;
	protected String description;
	protected boolean currentElement;
	protected String currentValue;

	/** Called to get tag characters ( ex:- <phoneNumber>0507362270</phoneNumber> -- to get 0507362270 characters )
	 * */
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if(currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		currentElement = false;
		if(localName.equals("phoneNumber")) {
			userName = currentValue;
		} else if(localName.equals("status")) {
			status = Integer.parseInt(currentValue);
		} else if(localName.equals("descr")) {
			description = currentValue;
		}
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
	}

	@Override
	public void processingInstruction(String target, String data) throws SAXException {
	}

	@Override
	public void setDocumentLocator(Locator locator) {
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		currentElement = true;
		// Root tag
//		if(localName.equals("message")) {
//			lastUpdated = Long.parseLong(atts.getValue("lastUpdated"));
//		}
	}

	@Override
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
	}

	public String getUserName() {
		return userName;
	}
	
	public int getStatus() {
		return status;
	}

	public String getDescription() {
		return description;
	}
}